<template>
  <!-- 一条文章单元格 -->
  <van-cell @click="articleDetailFn">
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ obj.title }}</span>
        <img
          class="thumb"
          v-lazy="obj.cover.images[0]"
          v-if="obj.cover.type == 1"
        />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="obj.cover.type > 1">
        <img
          class="thumb"
          v-for="(item, index) in obj.cover.images"
          :key="index"
          v-lazy="item"
        />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>作者: {{ obj.aut_name }}</span>
          <span>评论: {{ obj.comm_count }}</span>
          <span>发布日期: {{ timeAgo(obj.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon v-if="isShowCross" name="cross" @click.stop="show1 = true" />
        <!-- 一级反馈面板 -->
        <van-action-sheet
          cancel-text="取消"
          v-model="show1"
          :actions="actions1"
          @select="onSelect"
          get-container="body"
        />
        <!-- 二级反馈面板 -->
        <van-action-sheet
          cancel-text="返回"
          v-model="show2"
          :actions="actions2"
          @select="onSelect"
          get-container="body"
        />
        <!-- 用户反馈面板 -->
        <van-dialog
          v-model="show3"
          title="反馈其他问题"
          get-container="body"
          show-cancel-button
          :beforeClose="beforeClose"
        >
          <van-field
            v-focus
            placeholder="请输入反馈内容"
            type="textarea"
            v-model.trim="str"
          ></van-field>
        </van-dialog>
      </div>
    </template>
  </van-cell>
</template>

<script>
// 引入不感兴趣提示的组件
import { Toast } from 'vant'
import { articleDisLikeAPI, articleReportsAPI } from '@/api/home'
import { firstActions, secondActions } from '@/constant'
export default {
  name: 'ArticleItem',
  props: {
    obj: {
      type: Object,
      require: true
    },
    index: Number,
    // 这个父传子的属性,控制删除可见性
    isShowCross: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      str: '',
      show1: false,
      show2: false,
      show3: false,
      actions1: firstActions,
      actions2: secondActions
    }
  },
  methods: {
    async beforeClose (action, done) {
      if (action === 'confirm') {
        if (!this.str) {
          Toast.fail('内容不能为空!')
          done(false)
        } else {
          try {
            await articleReportsAPI({
              id: this.obj.art_id,
              type: 0,
              remark: this.str
            })
            Toast.success('反馈成功!')
          } catch (e) {
            Toast.fail('反馈失败!')
          }
        }
        done()
        this.show1 = this.show2 = false
      } else {
        done()
      }
    },

    async onSelect (ob) {
      if (ob.id === 22) {
        this.show2 = true
      } else if (ob.id === 11) {
        this.$emit('del_atc', this.index)
        this.show1 = false
        try {
          await articleDisLikeAPI(this.obj.art_id)
          Toast.success('操作成功!')
        } catch (err) {
          Toast.fail('操作失败!')
        }
      } else if (ob.id === 33) {
        console.log(123)
      } else {
        if (ob.id === 0) {
          this.show3 = !this.show3
        } else {
          try {
            await articleReportsAPI({
              id: this.obj.art_id,
              type: ob.id,
              remark: ob.name
            })
            Toast.success('反馈成功!')
          } catch (e) {
            Toast.fail('反馈失败!')
          }
        }
        this.show1 = this.show2 = false
      }
    },
    articleDetailFn () {
      // 点击文章跳转路由

      this.$router.push('/article?art_id=' + this.obj.art_id)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
